package com.gxa.shop.servlet.product;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gxa.shop.bean.pojo.Product;
import com.gxa.shop.bean.vo.ProductVo;
import com.gxa.shop.commons.r.R;
import com.gxa.shop.service.prodect.ProductService;
import com.gxa.shop.service.prodect.impl.ProductServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.stream.Collectors;

@WebServlet(name = "ProductSearchServlet",value = "/product/search")
public class ProductSearchServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        BufferedReader bufferedReader=req.getReader();
//        String  req_data=bufferedReader.lines().collect(Collectors.joining());
//        JSONObject listRegister= JSON.parseObject(req_data);
        String name=req.getParameter("name");

//        String name=listRegister.getString("name");

        ProductService productService=new ProductServiceImpl();
       List<ProductVo> productVoList= productService.findByName(name);
        R r;
        r=productVoList!=null?R.ok().addData("productVoList",productVoList):R.error("查询失败");
        String listResult= JSON.toJSONString(r);
        System.out.println(listResult);
        PrintWriter writer=resp.getWriter();
        writer.write(listResult);
        writer.flush();
        writer.close();

    }
}
